﻿//getNodeByID("节点id");
//getNodeByClass("节点className");    ########jquery
//closeNodeByClass("节点className");
//createNode("要创建的节点名");
//getJsonLength("json对象");
//changeStrToJsonOrArray("字符串", "键/默认为item1...", "是否数组形式 true or false", "要转换的格式 json or array");
//getQueryString("参数名");
//trimStr("字符串", "left or right/默认为两端");
//cout("字符串");
//getTimeNow("null or d or t or dt");//获取当前时间毫秒数、date 、time、datetime;
//changeNodeState("节点", "是否关闭", "是否打开");//返回节点最终显示状态;
//strToASII("字符串")字符串转ASII编码;
//asiiToStr("ASII码")ASII编码转字符串;
//getWindowSize()返回包含浏览器可视域宽高数组;
//nodeIndentation("要缩进节点", "缩进像素")节点缩进;
//upperOrLower("字符串", "null or upper or lower");//默认转为小写;
//getGUID() //生成GUID 
//InitialParameter(url) //读取json文件

//根据ID获取节点
function getNodeByID(nodeID) {
    return document.getElementById(nodeID);
}
//根据className获取节点
function getNodeByClass(className) {
    return $("." + className);
}
//按照类名关闭标签
function closeNodeByClass(className) {
    var nodes = getNodeByClass(className);
    for (var i = 0; i < nodes.length; i++) {
        nodes[i].style.display = "none";
    }
}
//创建节点
function createNode(tagName) {
    return document.createElement(tagName);
}
//获取json对象长度
function getJsonLength(json) {
    var length = 0;
    for (var key in json) {
        if (json[key].length) {
            length += json[key].length;
        }
        else {
            length++;
        }
    }
    return length;
}
//反序列化
function changeStrToJsonOrArray(str, name, isArr, toType) {
    if (!str) {
        if (toType == "json") {
            return {};
        }
        else if (toType=="array") {
            return [];
        }
    }
    if (isArr) {
        str = str.slice(2, str.length - 2);
    }
    else {
        str = str.slice(1, str.length - 1);
    }
    var thisResult;
    var strArr = str.split("},{");
    var jsonStr = null;
    var newJSON = null;
    if (toType == "json") {
        thisResult = {};
        var num = 0;
        var key = null;
        for (var i = 0; i < strArr.length; i++) {
            num++;
            jsonStr = "{" + strArr[i] + "}";
            newJSON = JSON.parse(jsonStr);
            if (name) {
                key = newJSON[name];
            }
            else {
                key = "item" + num;
            }
            thisResult[key] = newJSON;
        }
    }
    else if (toType=="array") {
        thisResult = new Array();
        for (var i = 0; i < strArr.length; i++) {
            jsonStr = "{" + strArr[i] + "}";
            newJSON = JSON.parse(jsonStr);
            thisResult.push(newJSON);
        }
    }
    return thisResult;
}
//获取地址栏参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var param = window.location.search.substr(1);
    var r = param.match(reg);
    if (r != null) return unescape(r[2]); return null;
}
//截取字符串空格
function trimStr(str, place) {
    if (place) {
        if (place == "left") {
            return str.replace(/(^\s*)/g, "");
        }
        else if (place == "right") {
            return str.replace(/(\s*$)/g, "");
        }
        else {
            return str.replace(/(^\s*)|(\s*$)/g, "");
        }
    }
    else {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    }
}
//控制台输出
function cout(str) {
    console.log(str);
}
//获取当前时间毫秒数
function getTimeNow(type) {
    var mydate = new Date();
    var thisResult = "";
    if (type) {
        switch (type) {
            case "d": thisResult = mydate.toLocaleDateString();
                break;
            case "t": thisResult = mydate.toLocaleTimeString();
                break;
            case "dt": thisResult = mydate.toLocaleString();
                break;
            default:
                thisResult = mydate.getTime();
                break;
        }
    }
    else {
        thisResult = mydate.getTime();
    }
    return thisResult;
}
//改变节点显示状态 
function changeNodeState(node, isclose, isopen) {
    if (isopen) {
        node.style.display = "";
        return true;
    }
    else if (isclose) {
        node.style.display = "none";
        return false;
    }
    else if (node.style.display == "none") {
        node.style.display = "";
        return true;
    }
    else {
        node.style.display = "none";
        return false;
    }
}
//保留小数
function myRound(number, length) {
    var numStr = "1";
    for (var i = 0; i < length; i++) {
        numStr += "0";
    }
    var num = parseInt(numStr);
    return Math.round(number * num) / num;
}
//字符串转ASII编码
function strToASII(str) {
    var thisResult = "";
    var str_char = null;
    var asii_char = null;
    for (var i = 0; i < str.length; i++) {
        str_char = str.slice(i, i+1);
        asii_char = str_char.charCodeAt();
        if (asii_char<10) {
            asii_char = "00" + asii_char;
        }
        else if (asii_char < 100) {
            asii_char = "0" + asii_char;
        }
        thisResult += asii_char;
    }
    return thisResult;
}
//ASII转字符串
function asiiToStr(asii) {
    var thisResult = "";
    var str_char = null;
    var asii_char = null;
    if (!isNaN(asii)) {
        if (asii<0||asii>127) {
            alert("字符串转换失败，输入的字符无效");
        }
        else {
            thisResult = String.fromCharCode(asii);
        }
    }
    else {
        for (var i = 0; i < asii.length / 3; i++) {
            asii_char = asii.slice(i * 3, (i + 1) * 3);
            str_char = String.fromCharCode(parseInt(asii_char));
            thisResult += str_char;
        }
    }
    
    return thisResult;
}
//获取屏幕宽高
function getWindowSize() {
    var thisResult = new Array();
    var height_window = document.documentElement.clientHeight;
    var width_window = document.documentElement.clientWidth;
    thisResult = [width_window, height_window];
    return thisResult;
}
//标签缩进
function nodeIndentation(node, num) {
    node.style.position = "relative";
    node.style.left = num + "px";
}
//大小写转换
function upperOrLower(str, type) {
    var thisResult = str.toLowerCase();
    if (type) {
        if (type=="upper") {
            thisResult = str.toUpperCase();
        }
    }
    return thisResult;
}
//生成GUID
function getGUID(length) {
    length = length ? length : 32;
    var s = [];
    var hexDigits = "0123456789abcdefABCDEF";
    for (var i = 0; i < length; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 22), 1);
    }
    s[12] = "4"; 
    s[16] = hexDigits.substr(('0x' + s[16] & 0x3) | 0x8, 1);
    var guid = s.join("");
    return guid; 
    //var s = [];
    //var hexDigits = "0123456789abcdefABCDEF";
    //for (var i = 0; i < 36; i++) {
    //    s[i] = hexDigits.substr(Math.floor(Math.random() * 22), 1);
    //}
    //s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    //s[19] = hexDigits.substr(('0x'+s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    //s[8] = s[13] = s[18] = s[23] = "-";
}
//读取Json文件
function InitialParameter(url) {
    var info = null;
    $.ajax({
        async: false,
        url: url,
        success: function (result) {
            info = result;
        },
        error: function (result) {
            console.log(result);
        }
    });
    return info;
}
//字符串替换
function replaceStr(str, oldStr, newStr) {
    return str.replace(/oldStr/g, newStr);
}
function getPageName() {
    var a = location.href.split("?")[0];
    var b = a.split("/");
    b = b[b.length - 1];
    return b;
}